#include "filetransferscrolldialog.h"
#include "ui_filetransferscrolldialog.h"
#include "filetransfermgrdlg.h"

FileTransferScrollDialog::FileTransferScrollDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::FileTransferScrollDialog),
    m_transfer_panel(new FileTransferMgrDlg())
{
    ui->setupUi(this);

    QObject::connect(this, SIGNAL(receiveFileTransferNotify(MsgBusParam)), m_transfer_panel.get(), SLOT(OnFileTransferNotify(MsgBusParam)));
    QObject::connect(this, SIGNAL(receiveOfflineFileNotify(MsgBusParam)), m_transfer_panel.get(), SLOT(OnOfflineFileNotify(MsgBusParam)));

    QObject::connect(m_transfer_panel.get(), SIGNAL(doNotifyOtherSide(MsgBusParam)), SLOT(OnDoNotifyOtherSide(MsgBusParam)));

    QObject::connect(m_transfer_panel.get(), SIGNAL(doOfflineNotifyOtherSide(MsgBusParam)), SLOT(OnDoOfflineNotifyOtherSide(MsgBusParam)));
    QObject::connect(m_transfer_panel.get(), SIGNAL(newactivity()), SLOT(OnTransferActivity()));
}

FileTransferScrollDialog::~FileTransferScrollDialog()
{
    delete ui;
}
void FileTransferScrollDialog::InitMsgHandler()
{
    AddHandler("im.frontend.imreceiver.filetransfer.notify", &FileTransferScrollDialog::OnIMFileTransferNotify, 0);
    // online picture
    AddHandler("im.frontend.imreceiver.pictransfer.finished", &FileTransferScrollDialog::OnIMPicTransferFinished, 0);
    // offline picture
    AddHandler("im.frontend.imreceiver.csfiletransfer.offlineimage.notify", &FileTransferScrollDialog::OnIMOfflineImageNotify, 0);
    // tribe picture
    AddHandler("im.frontend.imreceiver.csfiletransfer.tribeimage.notify", &FileTransferScrollDialog::OnIMTribeImageNotify, 0);
    // offline file
    AddHandler("im.frontend.imreceiver.csfiletransfer.offlinefile.notify", &FileTransferScrollDialog::OnIMOfflineFileNotify, 0);
    ui->scrollArea->setWidget(m_transfer_panel.get());
}

void FileTransferScrollDialog::CleanDataOnLogout()
{
    m_transfer_panel->ClearAllSession();
}

void FileTransferScrollDialog::SetWWMainUniqueId(const std::string &uniqueid)
{
    m_wwmain_unique = uniqueid;
}

bool FileTransferScrollDialog::OnIMFileTransferNotify(const std::string& msgid, MsgBusParam& param, bool& is_continue)
{
    emit receiveFileTransferNotify(param);
    return true;
}

bool FileTransferScrollDialog::OnIMPicTransferFinished(const std::string& msgid, MsgBusParam& param, bool& is_continue)
{
    emit receivePicTransferFinished(param);
    return true;
}

bool FileTransferScrollDialog::OnIMOfflineImageNotify(const std::string& msgid, MsgBusParam& param, bool& is_continue)
{
    emit receiveOfflineImageNotify(param);
    return true;
}
bool FileTransferScrollDialog::OnIMTribeImageNotify(const std::string& msgid, MsgBusParam& param, bool& is_continue)
{
    emit receiveTribeImageNotify(param);
    return true;
}
bool FileTransferScrollDialog::OnIMOfflineFileNotify(const std::string& msgid, MsgBusParam& param, bool& is_continue)
{
    emit receiveOfflineFileNotify(param);
    return true;
}

void FileTransferScrollDialog::OnDoNotifyOtherSide(MsgBusParam param)
{
    NetMsgBus::NetMsgBusSendMsg(m_wwmain_unique, "im.frontend.filetransfer.donotifyotherside",
        param, SendDirectToClient);
}

void FileTransferScrollDialog::OnDoOfflineNotifyOtherSide(MsgBusParam param)
{
    NetMsgBus::NetMsgBusSendMsg(m_wwmain_unique, "im.frontend.filetransfer.offlinefile.donotifyotherside",
        param, SendDirectToClient);
}

void FileTransferScrollDialog::OnTransferActivity()
{
    emit newactivity();
}
